#!/bin/bash
# deplacer vers l'azimuth DD MM
# usage movetoel DD (DD=degres)
#   
#    Copyright (C) 2010  Martin Aube
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#    Contact: martin.aube@cegepsherbrooke.qc.ca
#
let flag=0
verbose=1
let speed=1
let del1=0
if [ "$1" = "" ]
then echo "usage: movetoel DD MM Verbose ;  DD=degrees MM=minute Verbose={Silent=0}"
     exit 0
fi
if [ "$2" = "" ]
then echo "usage: movetoel DD MM Verbose ;  DD=degrees MM=minute Verbose={Silent=0}"
     exit 0
fi
var=$1
if [ "$(echo $var |grep "^[ [:digit:] ]*$")" ]
then echo " "
else echo "bad degree format"
     exit 0
fi
var=$2
if [ "$(echo $var |grep "^[ [:digit:] ]*$")" ]
then echo " "
else echo "bad minute format"
     exit 0
fi
if [ "$3" != "" ]
then verbose=$3
fi
let dest="$1"*60+"$2"
while [ "$flag" -eq 0 ]
do readmount "AZ" $verbose
   read D M S < mount.tmp 
   rm -f mount.tmp
   let min=D*60+M
   let delta=$dest-$min
   if [ "$delta" -ge 0 ]
       then
       if [ "$delta" -gt 10800 ]
       then let deltap=21600-$delta
            action="movewest"
       else let deltap=$delta
            action="moveeast"
       fi
   else
       if [ "$delta" -ge -10800 ]
       then let deltap=-$delta
            action="movewest"
       else let deltap=21600+$delta
            action="moveeast"
       fi      
   fi
    if [ "$deltap" -gt 300 ]
    then let speed=1
    fi
    if [ "$deltap" -le 300 ]
    then let speed=2
    fi
    if [ "$deltap" -le 60 ]
    then let speed=3
    fi
if [ "$del1" -eq "$deltap" ] 
        then
        if [ $verbose = "1" ] 
        then echo "arrived to destination"
        fi
        exit 0
fi 
if [ "$deltap" -eq 0 ] 
        then
        if [ $verbose = "1" ] 
        then echo "arrived to destination"
        fi
        exit 0
fi 
let del1=deltap
if [ $verbose = "1" ] 
then echo $action
fi
   let de=deltap/60
   let mi=deltap-60*de
   $action $de $mi $speed
sleep 0.2
done
